package com.littlebee.storysuper.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.littlebee.storysuper.R;

/**
 * 项目名称: CosmeticView
 * 包: hardware.pba.com.cosmeticview
 * 类描述: [自定义圆形进度倒计时]
 * 创建人: linwenbing
 * 创建时间: 17/1/10下午3:14
 * 修改人: linwenbing
 * 修改备注: [说明本次修改内容]
 * 版本: [v1.0]
 */
public class CostomCircleCountDown extends View{
    /**
     * 画笔
     */
    private Paint mPaint;

    /**
     * 圆位置
     */
    private RectF mRectF;

    /**
     * 圆边框角度，根据这个角度的变化来实现动画
     */
    private float mSweepAngle;
    /**
     * 动画进度
     */
    private int mProcessTime = 0;

    /**
     * 动画线程
     */
    private Handler mHandler;

    /**
     * 动画完成监听
     */
    private OnFinishAnimListener onFinishAnimListener;

    public void setOnFinishAnimListener(OnFinishAnimListener onFinishAnimListener) {
        this.onFinishAnimListener = onFinishAnimListener;
    }

    /********XML属性**********/
    private final int DEFAULT_PROCESS_WIDTH = 10;
    private final int DEFAULT_PROCESS_COLOR = Color.BLACK;
    private final int DEFAULT_CIRCLE_BG = Color.WHITE;
    private final int DEFAULT_TEXT_COLOR = Color.BLACK;
    private final int DEFAULT_TEXT_SIZE = 30;
    private final int DEFAULT_MAX_TIME = 5;
    /**
     * 进度条颜色
     */
    private int mProcessColor;
    /**
     * 进度条宽度
     */
    private float mProcessWidth;
    /**
     * 圆的背景颜色
     */
    private int mCircleBgColor;
    /**
     * 字体颜色
     */
    private int mTextColor;
    /**
     * 字体大小
     */
    private float mTextSize;
    /**
     * 提示文字
     */
    private String mTextValue;
    /**
     * 倒计时最大时间
     */
    private int mMaxTime;

    public CostomCircleCountDown(Context context) {
        super(context);
        init();
    }

    public CostomCircleCountDown(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CostomCircleCountDown);
        mProcessColor = ta.getColor(R.styleable.CostomCircleCountDown_cccd_process_coloc, DEFAULT_PROCESS_COLOR);
        mCircleBgColor = ta.getColor(R.styleable.CostomCircleCountDown_cccd_circle_bg_coloc,DEFAULT_CIRCLE_BG);
        mProcessWidth = ta.getDimension(R.styleable.CostomCircleCountDown_cccd_process_size,DEFAULT_PROCESS_WIDTH);
        mTextColor = ta.getColor(R.styleable.CostomCircleCountDown_cccd_text_coloc,DEFAULT_TEXT_COLOR);
        mTextSize = ta.getDimension(R.styleable.CostomCircleCountDown_cccd_text_size,DEFAULT_TEXT_SIZE);
        mTextValue = ta.getString(R.styleable.CostomCircleCountDown_cccd_text_value);
        mMaxTime = ta.getInteger(R.styleable.CostomCircleCountDown_cccd_max_time,DEFAULT_MAX_TIME);

        init();
    }

    private void init(){
        mPaint = new Paint();
        mRectF = new RectF();
        mPaint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = this.getWidth();
        int height = this.getHeight();
        setReftF(width,height);
        drawCircleBg(canvas);
        drawCircleStroke(canvas);
        drawText(canvas,width,height);
    }

    /**
     * 设置圆的位置
     * @param width
     * @param height
     */
    private void setReftF(int width,int height){
        mRectF.left = mProcessWidth;
        mRectF.top = mProcessWidth;
        mRectF.right = width - mProcessWidth/2;
        mRectF.bottom = height - mProcessWidth/2;
    }

    /**
     * 画圆背景
     * @param canvas
     */
    private void drawCircleBg(Canvas canvas){
        mPaint.setColor(mCircleBgColor);
        mPaint.setStrokeWidth(0);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawArc(mRectF,0,360,false,mPaint);
    }

    /**
     * 画圆进度
     */
    private void drawCircleStroke(Canvas canvas){
        mPaint.setColor(mProcessColor);
        mPaint.setStrokeWidth(mProcessWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(mRectF,0,mSweepAngle,false,mPaint);
    }

    private void drawText(Canvas canvas,int width,int height){
        drawUnitText(canvas,width,height);
        drawValueText(canvas,width,height);
    }

    /**
     * 画进度字符
     * @param canvas
     * @param width
     * @param height
     */
    private void drawValueText(Canvas canvas,int width,int height){
        mPaint.setTextSize(mTextSize);
        mPaint.setColor(mTextColor);
        mPaint.setStyle(Paint.Style.FILL.FILL);
        String value = mProcessTime + "s";
        int textWidth = (int) mPaint.measureText(value, 0, value.length());
        int textUnitWidth = (int) mPaint.measureText(value, 0, mTextValue.length());
        canvas.drawText(value,width/2 - textWidth/2,height/2 + textUnitWidth,mPaint);
    }

    /**
     * 画字符说明
     * @param canvas
     * @param width
     * @param height
     */
    private void drawUnitText(Canvas canvas,int width,int height){
        if (!TextUtils.isEmpty(mTextValue)){
            mPaint.setTextSize(mTextSize);
            mPaint.setColor(mTextColor);
            mPaint.setStyle(Paint.Style.FILL.FILL);
            int textWidth = (int) mPaint.measureText(mTextValue, 0, mTextValue.length());
            canvas.drawText(mTextValue,width/2 - textWidth/2,height/2,mPaint);
        }
    }

    /**
     * 开始倒计时
     */
    public void startAnim(){
        if (mHandler == null){
            mHandler = new Handler();
            final int time  = mMaxTime * 1000/360*3;
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSweepAngle +=3;
                    postInvalidate();
                    mProcessTime = mMaxTime - (int) mSweepAngle/(360/mMaxTime) + 1;
                    if (mSweepAngle == 360){
                        if (onFinishAnimListener != null){
                            onFinishAnimListener.onFinish();
                        }
                        stopAnim();
                    }else{
                        mHandler.postDelayed(this,time);
                    }
                }
            },time);
        }
    }

    public void setmMaxTime(int value){
        if (value > 1){
            mMaxTime  = value - 1;
        }else{
            mMaxTime = 1;
        }
    }

    public void stopAnim(){


        cancleHandler();
    }

    public void cancleHandler(){
        mHandler.removeCallbacksAndMessages(null);
    }

    public void setProgressNotInUiThread(int progress) {
        this.mSweepAngle = (progress / mMaxTime) * 360;
        this.postInvalidate();
    }

    public interface OnFinishAnimListener{
        void onFinish();
    }
}
